﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class Add_UserSignature : BasePage
    {
        private Sys_SignatureBLL ssb = new Sys_SignatureBLL();
        private Sys_UserBLL sub = new Sys_UserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    hfldUserId.Value = Request.QueryString["ID"].ToString();
                    Bind();
                }
            }
        }

        private void Bind()
        {
            string con = hfldUserId.Value;
            con += ",1";
            DataTable dt = ssb.GetInfoByCon("Condition", con);
            gvwSignature.DataSource = dt;
            gvwSignature.DataBind();

            lblUserName.Text = sub.GetInfoById(hfldUserId.Value).USER_NAME;
            if (dt.Rows.Count > 0)
            {
                txtSignatureName.Text = dt.Rows[0]["SIGNATURENAME"].ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (gvwSignature.Rows.Count < 1)
            {
                string path = Server.MapPath("~/UpLoad") + "\\" + fupImage.FileName;
                if (!File.Exists(path))
                {
                    fupImage.SaveAs(Server.MapPath("~/UpLoad") + "\\" + fupImage.FileName);
                }
                Sys_Signature model = new Sys_Signature();

                model.SYSUSERID = decimal.Parse(hfldUserId.Value);
                model.SIGNATURETYPE = decimal.Parse(radioType.SelectedValue);
                // model.SIGNATUREPWD = CommandHelper.Md532(txtSignaturePwd.Text.Trim() + "www.thinkersoft.cc");
                model.SIGNATUREPWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtSignaturePwd.Text.Trim(), "MD5");
                model.SIGNATUREPATH = "~/UpLoad/" + fupImage.FileName;
                model.STATE = 1;
                model.SIGNATURENAME = txtSignatureName.Text;

                int count = ssb.INSERT(model, lognUser.USER_NAME);

                if (count > 0)
                {
                    MessageBox.Show(Page, "设置成功");
                    string con = hfldUserId.Value;
                    con += ",1";
                    DataTable dt = ssb.GetInfoByCon("Condition", con);
                    gvwSignature.DataSource = dt;
                    gvwSignature.DataBind();
                    return;
                }
                else
                {
                    MessageBox.Show(Page, "设置失败");
                    return;
                }
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("SelectUser.aspx");
        }

        protected void gvwSignature_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string sigatureID = e.Row.Cells[0].Text.Trim();

                //Image image = e.Row.FindControl("imgHad") as Image;
                //image.ImageUrl = "SignatureImage.aspx?SIGNATUREID=" + sigatureID + "";
                //if (e.Row.Cells[1].Text == "1")
                //{
                //    e.Row.Cells[1].Text = "私章";
                //}
                //else
                //{
                //    e.Row.Cells[1].Text = "公章";
                //}
                if (e.Row.Cells[1].Text == "公章")
                {
                    e.Row.Cells[1].Text = "公章";
                }
                else
                {
                    e.Row.Cells[1].Text = "私章";
                }
            }
        }

        protected void gvwSignature_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteSignature")
            {
                decimal signatrueID = decimal.Parse(e.CommandArgument.ToString().Trim());
                int count = ssb.DELETE(signatrueID.ToString(), lognUser.USER_NAME);
                if (count > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('删除成功');", true);
                }
                string con = hfldUserId.Value;
                con += ",1";
                DataTable dt = ssb.GetInfoByCon("Condition", con);
                gvwSignature.DataSource = dt;
                gvwSignature.DataBind();
            }
        }

        protected void radlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = radioType.SelectedValue.ToString();
            DataTable dtSignature = new Sys_SignatureBLL().SelectBySign(decimal.Parse(hfldUserId.Value.ToString()), decimal.Parse(type));
            gvwSignature.DataSource = dtSignature;
            gvwSignature.DataBind();
        }
    }
}